// pages/seekCabinet/seekCabinet.js
var util = require('../../utils/util.js')
var app = getApp()
var rootUrl = app.globalData.rootDocment
var imgPublicUrl = rootUrl + 'Public/Home/images/'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //点击标记点要设置的数据
    latitude:0,  //定义维度
    longitude:0, //定义经度
    //标记信息数组
    markers: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //loading加载动画
		var that = this
     //请求坐标位置
	  //发起网络请求
	  util.AJAX('Home/Location/results', {}, function (res) { //方法体
		  var datarow = res.data.data
      if (datarow){
        var datalen = res.data.data.length

        if (datalen) {
          var newrow = []
          for (var i = 0; i < datalen; i++) {
            var lsarray = {
              iconPath: "/images/markersIcon.png",
              id: 0,
              latitude: datarow[i].latitude,
              longitude: datarow[i].longitude,
              addressName: datarow[i].addressName,//地址名称
              detailAddress: datarow[i].detailAddress, //详细地址
              width: 35,
              height: 35,
              callout: {
                content: datarow[i].addressName,
                bgColor: "#f1f1f1",
                borderRadius: "5",
                display: "ALWAYS",
                padding: "10"
              }
            }
            //加入
            newrow.push(lsarray)
          }
          that.setData({ markers: newrow })
        }
      }
	  }, 'POST')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  app.publicchangenavigate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	return app.globalshare();
  },
  callouttap: function (e) {
    var that = this
    //获取用户当前的授权状态
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userLocation']) {
          that.getLocationfun(e)
        } else { //授权状态关闭
          if (res.authSetting['scope.userLocation'] == null) { //未发起授权
            wx.authorize({
              scope: 'scope.userLocation',
              success() {
                wx.getSetting({
                  success: function (res) {
                    if (res.authSetting['scope.userLocation']) {
                      that.getLocationfun(e)
                    }
                  }
                })
              }
            })            
          } else { //拒绝授权
            wx.openSetting({
              success: (res) => {
                if (res.authSetting['scope.userLocation']) { //重新授权成功
                  that.getLocationfun(e)
                } else {//退回
                }
              }
            })
          }
        }
      }
    })
  },
  //获取地理位置
  getLocationfun:function(e){
    var markerid = e.markerId
    wx.showLoading({
      title: '玩命加载中...',
    })
    var that = this;
    var latitudes = parseFloat(that.data.markers[markerid].latitude)
    var longitudes = parseFloat(that.data.markers[markerid].longitude)
    var addressName = that.data.markers[markerid].addressName
    var detailAddress = that.data.markers[markerid].detailAddress
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        wx.hideLoading()
        wx.openLocation({
          latitude: latitudes,
          longitude: longitudes,
          name: addressName,
          address: detailAddress,
          scale: 10
        })
      }
    })
  }
})